<?php

namespace App\Providers;

use Elastic\Elasticsearch\Client;
use Illuminate\Support\ServiceProvider;
use Elastic\Elasticsearch\ClientBuilder;

class ElasticsearchServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->singleton(Client::class, function ($app) {
            if (config('elasticsearch.user')) {
                $hosts = [
                    config('elasticsearch.scheme') . '://' .
                    config('elasticsearch.user') . ':' .
                    config('elasticsearch.pass') . '@' .
                    config('elasticsearch.host'). ':' .
                    config('elasticsearch.port')
                ];
            }else{
                $hosts = [config('elasticsearch.scheme'). '://'.config('elasticsearch.host'). ':'. config('elasticsearch.port')];
            }
            return ClientBuilder::create()->setHosts($hosts)->build();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
